package demo06;

import java.util.Random;

class Maze {
    private char[][] grid;
    private int playerX;
    private int playerY;
    private Player player;

    public Maze(int size) {
        grid = new char[size][size];
        player = new Player();
        initializeMaze();
        placePlayer();
    }

    private void initializeMaze() {
        Random rand = new Random();
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                double chance = rand.nextDouble();
                if (chance < 0.2) {
                    grid[i][j] = '#'; 
                } else if (chance < 0.3) {
                    grid[i][j] = 'M'; 
                } else if (chance < 0.4) {
                    grid[i][j] = 'H'; 
                } else if (chance < 0.5) {
                    grid[i][j] = 'W'; 
                } else if (chance < 0.6) {
                    grid[i][j] = 'A'; 
                } else {
                    grid[i][j] = '.'; 
                }
            }
        }
        grid[grid.length-1][grid[0].length-1] = 'E'; 
    }

    private void placePlayer() {
        playerX = 0;
        playerY = 0;
        grid[playerX][playerY] = 'P';
    }

    public void display() {
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                System.out.print(grid[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("Health: " + player.getHealth());
    }

    public boolean movePlayer(char direction) {
        int newX = playerX;
        int newY = playerY;

        switch (direction) {
            case 'w': newX--; break;
            case 'a': newY--; break;
            case 's': newX++; break;
            case 'd': newY++; break;
            default: return false;
        }

        if (newX < 0 || newX >= grid.length || newY < 0 || newY >= grid[0].length) {
            return false;
        }

        char destination = grid[newX][newY];
        if (destination == '#') {
            return false;
        }

        if (destination == 'M') {
            Monster monster = new Monster();
            BattleSystem battle = new BattleSystem(player, monster);
            if (!battle.startBattle()) {
                return false;
            }
        } else if (destination == 'H') {
            player.addHealthPotion();
            System.out.println("Found health potion! Total: " + player.getHealthPotions());
        } else if (destination == 'W') {
            player.upgradeWeapon();
            System.out.println("Found weapon upgrade! Attack: " + player.getAttack());
        } else if (destination == 'A') {
            player.upgradeArmor();
            System.out.println("Found armor upgrade! Defense: " + player.getDefense());
        } else if (destination == 'E') {
            System.out.println("Congratulations! You escaped the maze!");
            System.exit(0);
        }

        grid[playerX][playerY] = '.';
        playerX = newX;
        playerY = newY;
        grid[playerX][playerY] = 'P';
        return true;
    }

    public void showPlayerStats() {
        player.displayStats();
    }
}

